<!-- 退单 -->

<template>
	<fui-form>
		<custom-form-item-card label="退单原因" required>
			<fui-radio-group v-model="formData.reason">
				<fui-label v-for="item in reasonList" :key="item.value">
					<fui-list-cell>
						<view class="fui-align__center">
							<fui-radio :value="item.value"></fui-radio>
							<fui-text class="m-l-16" :text="item.label"></fui-text>
						</view>
					</fui-list-cell>
				</fui-label>


			</fui-radio-group>
			<fui-form-item asterisk label="详细描述" label-width="auto" :bottom-border="false">
				<template #vertical>
					<fui-textarea v-model="formData.remark" :border-top="false" placeholder="详细描述"></fui-textarea>
				</template>
			</fui-form-item>
		</custom-form-item-card>
	</fui-form>

</template>

<script>
	import {
		defineComponent
	} from "vue"
	import {
		mapState,
		mapWritableState
	} from 'pinia'
	import {
		useHandleWorkOrder_ChargeBack
	} from "../../../../store/modules/handleWorkOrder_ChargeBack"
	import {
		useSyStemStore
	} from "../../../../store/modules/system"
	import {
		getDicValue
	} from "../../../../utils/util"
	import {
		DIC_KEY
	} from "../../../../config/dicKey-config"
	import form from '@/components/firstui/fui-validator/fui-validator'
	import _ from 'lodash'
	import {
		WorkOrderMyIndexApi
	} from "../../../../api/workOrder"
	export default defineComponent({
		data() {
			return {

			}
		},
		computed: {
			...mapWritableState(useHandleWorkOrder_ChargeBack, ['formData']),
			...mapState(useSyStemStore, ['dicList']),
			reasonList() {
				return getDicValue(this.dicList, DIC_KEY.obs_return_reception)

			},

		},
		emits: ['disabled'],
		methods: {
			async submit(orderNum, cb) {
				const rules = [{
					name: "reason",
					rule: ["required"],
					msg: ["请选择退单原因"]
				}, {
					name: "remark",
					rule: ["required"],
					msg: ["请填写退单说明"]
				}];
				try {
					this.formData.orderNum = orderNum
					let {
						isPassed,
						errorMsg
					} = form.validator(this.formData, rules)
					if (isPassed) {
						this.$emit('disabled', true)
						await WorkOrderMyIndexApi.chargeback(this.formData)
						this.fui.toast('退单成功')
						_.delay(() => {
							this.formData = {
								// 退单原因
								reason: '',
								// 退单说明
								remark: '',
								// 订单号
								orderNum: '',
							}
							cb && cb()
						}, 500)
					} else {
						this.$emit('disabled', false)
						this.fui.toast(errorMsg)
					}
				} catch (err) {
					this.$emit('disabled', false)
					console.error(err);
					this.fui.toast(err)

				}
			}
		},
	})
</script>

<style>
</style>